﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class my_new_img_add : AdminpageBase
{
    private string mypath = "";
    private string mypathBak = "";
    protected string strtitle = "";
    protected Model.NewsSite model;
    protected string ctype = null;
    protected long long_eid = 0;
    protected int c_id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            inidata();      
        }
    }
    private void inidata()
    {
        string eid = "";
        ctype = ReadRequest.RequestString("t");
        eid = ReadRequest.RequestString("eid");
        c_id = ReadRequest.RequestInt("c_id");
        try
        {
            txtctype.Text = ctype;
            txtp_type.Text = c_id.ToString();   
            if (ctype != null && ctype.Length > 3 && "edit".Equals(ctype))
            {
                long_eid = long.Parse(eid);
                txtlong_eid.Text = long_eid.ToString();
                model = BLL.NewsSite.GetModel(long_eid);
                txtTitle.Text = "";
                if (model != null)
                { 
                    txtContent.Value = "";
                }
            }
            else
            {
                ctype = "ad";
            }
        }
        catch {
            ctype = "ad";
        }
        Model.Newsclass nclass = BLL.Newsclass.GetModel(c_id);
        if (nclass != null)
        {
            strtitle = nclass.classname;
            lblshow.Text = strtitle;
        }
        if (long_eid < 1)
        {
            ctype = "ad";
        }
    }

    //submit
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        Up_Image();
    }
    protected void Up_Image()
    {
        bool fileOk = false;
        string d = DateTime.Now.ToString("yyyyMMddHHmmss");
        string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
        string path = Server.MapPath("/userfiles/image/") + d + fileExtension;
        string pathBak = Server.MapPath("/userfiles/imageBak/") + d + fileExtension;
        if (FileUpload1.HasFile)
        {
            string[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg", "jpeg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOk = true;
                }
            }
            if (fileOk)
            {
                if (myimg.ImageUrl != "/userfiles/temp/default.jpg")
                {
                    Obj.file_delete(myimg.ImageUrl);
                }
                PhotoControl.MakeThumbnail(FileUpload1.PostedFile, path, 1600, 1200);
                PhotoControl.MakeThumbnail(FileUpload1.PostedFile, pathBak, 325, 207);

                mypath = "/userfiles/image/" + d + fileExtension;
                mypathBak = "/userfiles/imageBak/" + d + fileExtension;
                addinfo(mypath, mypathBak);
            }
            else
            {
                lblmsg.CssClass = "errmsg";
                lblmsg.Text = "X 图片格式错误。";
            }
        }
        else
        {
            mypath = myimg.ImageUrl;
            mypathBak = myimg.ImageUrl;
            addinfo(mypath, mypathBak);
        }
    }
    protected void addinfo(string mypath, string mypathBak)
    {
        Model.NewsSite Info = new Model.NewsSite();
        Info.Title = txtTitle.Text;
        Info.Contents = txtContent.Value; 
        BLL.NewsSite.Add(Info);        
        Response.Redirect("/manage/my_news_list.aspx?cid=" + txtp_type.Text);
    }
}